﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EADCA3MVCApp.Models
{
    public partial class CloudService
    {
         CloudEntities1 db = new CloudEntities1();
        
        // Properties
        public int NoOfInstances { get; set; }

        

        // Static list of Instance Sizes
        public static string[] Sizes
        {
            get
            {
                return new string[] { "Very Small", "Small", "Medium", "Large", "Very Large" };
            }
        }
        
        // method to calclate saving
        public double Saving(double cost)
        {
            double saving = cost - (double)this._MonthlyCost;
            return saving;
        }

        // total cost = monthly cost x no of instances
        public double TotalCost { get; set; }

        public void GetTotalCost(double noOfInst )
        {
            this.TotalCost = (double)this.MonthlyCost * noOfInst;
        }
               
        public List<CloudService> GetApplicableService(string instanceSize)
        {
            var services = db.CloudServices.Where(cs => cs.InstanceSize == instanceSize).OrderBy(cs => cs.MonthlyCost).ToList();
            return services;
        }
       
        public CloudService GetCheapestService(string instanceSize)
        {
            var services = db.CloudServices.Where(cs => cs.InstanceSize == instanceSize).OrderBy(cs => cs.MonthlyCost).ToList();

            decimal cheapest = decimal.MaxValue;

            foreach (var c in services)
            {
                if (c.MonthlyCost <= cheapest)
                {
                    cheapest = c.MonthlyCost;
                }
            }

            CloudService cheapestService = db.CloudServices.Where(csv => csv.MonthlyCost == cheapest).Single(); 

            return cheapestService;
        }
    }
}

